package cn.kai.user.service.impl;

import cn.kai.auth.domain.Logininfo;
import cn.kai.auth.mapper.LogininfoMapper;
import cn.kai.basic.constants.BaseConstant;
import cn.kai.basic.dto.RegisterDto;
import cn.kai.basic.exception.BusinessException;
import cn.kai.basic.util.MD5Utils;
import cn.kai.basic.util.StrUtils;
import cn.kai.user.domain.User;
import cn.kai.user.mapper.UserMapper;
import cn.kai.user.service.IUserService;
import cn.kai.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kai
 * @since 2022-06-16
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 电话号码注册
     * @param dto
     */
    @Override
    public void registerPhone(RegisterDto dto) {
        String phone = dto.getPhone();
        String smsCode = dto.getSmsCode();
        String password = dto.getPassword();
        String passwordRepeat = dto.getPasswordRepeat();

        //空校验
        if (phone==null || smsCode==null || password==null || passwordRepeat==null){
            throw new BusinessException("请输入完整信息!");
        }
        //校验短信验证码是否正确
        String smsCodeTemp = redisTemplate.opsForValue().get(BaseConstant.RegisterConstants.REGISTER_SMS_PREFIX+phone).toString().split(":")[0];
        if (smsCode==null){
            //拿不到验证码
            throw new BusinessException("验证码已失效，请重新获取！");
        }else if (!smsCode.equalsIgnoreCase(smsCodeTemp)){
            throw new BusinessException("短信验证码不正确，请重新输入！");
        }
        //校验两次密码是否一致
        if (!password.equals(passwordRepeat)){
            throw new BusinessException("两次输入的密码不相等，请重新输入！");
        }
        //保存数据 t_user + t_logininfo
        User user = dto2User(dto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    /**
     * 封装的类型转换方法
     * @param user
     * @return
     */
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        //手动设置账户type类型
        logininfo.setType(1);
        return logininfo;
    }

    /**
     * 封装的类型转换方法
     * @param dto
     * @return
     */
    private User dto2User(RegisterDto dto) {
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }
}
